#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
 
#include "data_type.h"
#include "alloc.h"
#include "memfunc.h"
#include "basefunc.h"
#include "struct_deal.h"
#include "crypto_func.h"
#include "memdb.h"
#include "message.h"
#include "ex_module.h"
#include "sys_func.h"
#include "tcm_constants.h"
#include "tcm_structures.h"
#include "tcmfunc.h"
#include "tcm_cube_struct.h"
#include "tcm_key_manage.h"
#include "tcm_key_desc.h"
#include "tcm_pik_desc.h"
#include "tcm_view.h"
#include "pik_send.h"
// add para lib_include

   RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset_get_byname (char * name);

int pik_send_init(void * sub_proc, void * para)
{
	int ret;

	// add yorself's module init func here
	return 0;
}
int pik_send_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	
	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(MESSAGE))&&(subtype==SUBTYPE(MESSAGE,INSTANCE_INFO)))
		{
			ret=proc_pik_info(sub_proc,recv_msg);
		}
	}
	return 0;
}
int proc_pik_info(void * sub_proc,void * recv_msg)
{
	int ret=0;
	int fd;
	RECORD(MESSAGE,INSTANCE_INFO)  * pik_userinfo;
	RECORD(TCM_PIK_QUERY,PIK) * pik_query;
	RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset;
	void * new_msg;

	ret=message_get_record(recv_msg,&pik_userinfo,0);
	if(ret<0)
		return -EINVAL;
	if(pik_userinfo==NULL)
		return -EINVAL;

    ret=proc_share_data_getvalue("uuid",pik_userinfo->node_uuid);
	if(ret<0)
		return ret;
	local_keyset=local_keyset_get_byname(pik_userinfo->user_name);
    if(local_keyset==NULL)
        return -EINVAL;
	else
	{
		printf("local_name:%s\n",local_keyset->user_name);
		printf("local_pikuuid:%s\n",local_keyset->pik_uuid);
			int j=0;
			for(j =0; j < 32; j++){
			printf("%0x",local_keyset->pik_uuid[j]);
			if((j+1)%4==0){
				printf(" ");
			}
    	}
		printf(" \n");
		printf("local_certuuid:%s\n",local_keyset->pikcert_uuid);
		printf("local_signkeyuuid:%s\n",local_keyset->signkey_uuid);
			for(j =0; j < 32; j++){
			printf("%0x",local_keyset->signkey_uuid[j]);
			if((j+1)%4==0){
				printf(" ");
			}
    	}
		printf("local_unbindkeyuuid:%s\n",local_keyset->unbindkey_uuid);
			for(j =0; j < 32; j++){
			printf("%0x",local_keyset->unbindkey_uuid[j]);
			if((j+1)%4==0){
				printf(" ");
			}
    	}
	}

	char file_name[DIGEST_SIZE*4];
	BYTE * Buf1;
	Memset(file_name,0,DIGEST_SIZE*3);
	Strcpy(file_name,"pik_info/");
	Strcat(file_name,pik_userinfo->user_name);
	fd=open(file_name,O_RDONLY);
    Buf1=Talloc0(DIGEST_SIZE*32+1);
	ret=read(fd,Buf1,DIGEST_SIZE*32);
	close(fd);
	printf("看这里：%s\n",Buf1);
	pik_query=Talloc0(sizeof(*pik_query));
	if(pik_query==NULL)
	{
		printf("分配失败\n");
		return -ENOMEM;	

	}
	else
	{
		printf("成功\n");
	}
	Strncpy(pik_query->username,pik_userinfo->user_name,DIGEST_SIZE);
	printf("看这里：%s\n",pik_query->username);
	Memcpy(pik_query->user_role,pik_userinfo->proc_name,DIGEST_SIZE);
	printf("看这里：%s\n",pik_query->user_role);
	Memcpy(pik_query->node_uuid,pik_userinfo->node_uuid,DIGEST_SIZE);

		int j=0;
			for(j =0; j < 32; j++){
			printf("%0x",pik_query->node_uuid[j]);
			if((j+1)%4==0){
				printf(" ");
			}
    	}
		printf(" \n");

	// printf("看这里：%s\n",pik_query->node_uuid);
	Memcpy(pik_query->pik_uuid,Buf1,DIGEST_SIZE);
	// printf("看这里：%s\n",pik_query->pik_uuid);
	 j=0;
			for(j =0; j < 32; j++){
			printf("%0x",pik_query->pik_uuid[j]);
			if((j+1)%4==0){
				printf(" ");
			}
    	}
    Memcpy(pik_query->pikcert_uuid,local_keyset->pikcert_uuid,DIGEST_SIZE);
	Memcpy(pik_query->signkey_uuid,local_keyset->signkey_uuid,DIGEST_SIZE);
	Memcpy(pik_query->unbindkey_uuid,local_keyset->unbindkey_uuid,DIGEST_SIZE);

	new_msg=message_create(TYPE_PAIR(TCM_PIK_QUERY,PIK),recv_msg);	
	if(new_msg==NULL)
		return -EINVAL;
	else
	{
		printf("成功\n");
	}
    ret=message_add_record(new_msg,pik_query);
    if(ret<0)
        return ret;
	else
	{
		printf("成功\n");
	}
	ret=ex_module_sendmsg(sub_proc,new_msg);
	if(ret<0)
		return ret;	
	else
	{
		printf("成功\n");
	}
	return ret;

	
}
RECORD(TCM_KEY_DESC,LOCAL_KEYSET) * local_keyset_get_byname (char * name)
{
	int ret;
	RECORD(TCM_KEY_DESC,LOCAL_KEYSET ) * local_keyset;
	DB_RECORD * db_record;
	char buf[DIGEST_SIZE];
	Memset(buf,0,DIGEST_SIZE);
	Strncpy(buf,name,DIGEST_SIZE);
	db_record=memdb_find_first(TYPE_PAIR(TCM_KEY_DESC,LOCAL_KEYSET),"user_name",buf);

	if(db_record==NULL)
		return NULL;
	return db_record->record;
}